import type { RouteRecordNormalized } from 'vue-router';
import axiosInstance from '@/api/axiosRequest';
import { baseURL } from '@/api/base';
import { UserState } from '@/store/modules/user/types';
import { getToken,setToken } from '@/utils/auth';
import axios from 'axios';


const axiosInstance2 = axios.create({
  baseURL: baseURL,
  timeout: 12000
});

export interface LoginData {
  username: string;
  password: string;
}

export interface LoginRes {
  code: number,
  message: string;
  result: {
    access_token: string
  }
}

export interface getInfoRes {
  code: number,
  message: string;
  result: UserState
}

export function login(data: LoginData) {
  let formData = new FormData();
  formData.append("username", data.username);
  formData.append("password", data.password);
  return axiosInstance2.post<LoginRes>('/uac/auth/form', formData,{
    headers:{
      'Content-type': "application/x-www-form-urlencoded",
      Authorization:'Basic Y21ueS1jbGllbnQtdWFjOmNtbnlDbGllbnRTZWNyZXQ='
    }
  })
}

export function refreshToken(data: LoginData) {
  let formData = new FormData();
  formData.append("username", data.username);
  formData.append("password", data.password);
  return axiosInstance.post<LoginRes>('/uac/auth/user/refreshToken', formData,{
    headers:{
      'Content-type': "application/x-www-form-urlencoded",
      'Authorization':'Basic Y21ueS1jbGllbnQtdWFjOmNtbnlDbGllbnRTZWNyZXQ='
    }
  })
}
refreshToken

export function logout() {
  return axiosInstance.post<LoginRes>('/uac/user/logout',{});
}

export function getUserInfo() {
  return axiosInstance.post<getInfoRes>('/uac/user/getInformation',{})
}

export function getMenuList() {
  return axiosInstance.post<RouteRecordNormalized[]>('/api/user/menu');
}
